﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Student_userinfo : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    private string wbh;
    protected void Page_Load(object sender, EventArgs e)
    {
        BinData();
    }
    private void BinData()
    {
        string wbh = Session["wbh"].ToString();
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      select p).ToList().First();
        tb_name.Text = result.sname;
        tb_phone.Text = result.sphone;
        tb_sidfy.Text = result.sidfy;
        tb_email.Text = result.semail;
    }
    protected void bt_confirm_Click(object sender, EventArgs e)
    {
        string pwd = tb_Pwd.Text.ToString().Trim();
        string wbh = Session["wbh"].ToString();
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      where p.proflag < 2
                      select p).ToList();
        if (result.Count < 1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('报名信息已提交，无法修改个人信息！');window.location='../main.aspx'</script>");
            return;
        }
        if (db.proc_LoginCheck(result.First().sphone, pwd) == 0)
        {
            tb_name.Text = result.First().sname;
            tb_sidfy.Text = result.First().sidfy;
            tb_email.Text = result.First().semail;
            pl_CheckPwd.Visible = false;
            pl_StuInfo.Visible = true;
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('密码输入错误！');</script>");
        }
    }

    protected void bt_Submit_Click(object sender, EventArgs e)
    {
        string wbh = Session["wbh"].ToString();
        var result = (from p in db.proc_GetStuInfobyPwd(tb_phone.Text)
                      where p.wbh != wbh
                      select p).ToList();
        var SidfyRes = (from p in db.proc_GetStuInfo()
                        where p.sidfy == tb_sidfy.Text.ToString().Trim() && p.wbh != wbh
                        select p).ToList();
        if (result.Count() > 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('该手机号已被注册！');</script>");
            return;
        }
        if (SidfyRes.Count > 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('该身份证号已被注册！');</script>");
            return;
        }
        if (db.proc_UpdateStuBywbh(wbh, tb_phone.Text.ToString().Trim(), tb_name.Text.ToString().Trim(), tb_sidfy.Text.ToString().Trim(), tb_email.Text.ToString().Trim()) == -1)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('更新失败，请重试！');</script>");
        }
        else
        {
            db.SubmitChanges();
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('更新成功！');</script>");
        }
    }
}